package com.lh.api.controller.user;

import com.lh.api.annotation.Login;
import com.lh.api.controller.AbstractController;
import com.lh.api.form.AddressForm;
import com.lh.api.form.DefaultAddressForm;
import com.lh.api.service.UserAddressService;
import com.lh.api.service.UserService;
import com.lh.api.vo.UserAddrVo;
import com.lh.common.utils.AssertUtil;
import com.lh.common.utils.Constant;
import com.lh.common.utils.RegexUtils;
import com.lh.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gze
 * @ClassName: UserAddressAction
 * @Description: 收获地址管理
 * @date: 2020/5/13 0013 14:18
 */
@RestController
@RequestMapping("/api/userAddress")
@Api(value="UserAddressAction",tags={"收获地址管理"})
public class UserAddressAction extends AbstractController {

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查询用户地址")
    @GetMapping("/getList")
    @Login
    public Result<List<UserAddrVo>> getUserAddress(){
        Long userId = super.getUserId();
        List<UserAddrVo> userAddrVos = userAddressService.selectByUserId(userId);
        return Result.ok(userAddrVos);
    }

    @ApiOperation(value = "添加地址")
    @PostMapping("/addAddress")
    @Login
    public Result<String> addAddress(@RequestBody @Validated AddressForm addressForm){
        AssertUtil.assertNotFalse(RegexUtils.checkMobile(addressForm.getPhone()), Constant.RESULT.FI1000, "Mobile No. format does not match");

        userAddressService.addObj(addressForm, getUserId());
        return Result.ok();
    }

    @ApiOperation(value = "修改地址")
    @PostMapping("/updateAddress")
    @Login
    public Result<String> updateAddress(@RequestBody @Validated AddressForm addressForm){
        AssertUtil.assertNotNull(addressForm.getUaid(), Constant.RESULT.FI1000, "uaid is required");
        AssertUtil.assertNotFalse(RegexUtils.checkMobile(addressForm.getPhone()), Constant.RESULT.FI1000, "Mobile No. format does not match");

        userAddressService.updateObj(addressForm);
        return Result.ok();
    }

    @ApiOperation(value = "设置默认")
    @PostMapping("/setDefault")
    @Login
    public Result<String> updateAddress(@RequestBody @Validated DefaultAddressForm defaultAddressForm){
        userAddressService.setDefault(defaultAddressForm, getUserId());
        return Result.ok();
    }

    @ApiOperation(value = "获得默认地址")
    @GetMapping("/getDefault")
    @ResponseBody
    @Login
    public Result<UserAddrVo> getDefaultAddress(){
        return Result.ok(userService.getDefaultAddress(getUserId()));
    }

    @ApiOperation(value = "删除用户地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "uaid", value = "地址id", required = true,dataTypeClass= Long.class)
    })
    @GetMapping(value = "/delAddress")
    @ResponseBody
    @Login
    public Result<String> delAddress(Long uaid){
        userAddressService.delAddress(getUserId(), uaid);
        return Result.ok();
    }
}
